################################
###                          ###
### Women and Party Building ###
###                          ###
###      Master Script       ###
###                          ###
################################


# This script executes all of the other .R scripts in the correct order.
# Each script can also be run individually.

# Loads required packages and installs them if not already installed
                                                                            #The versions that we used:
if (!require('genderBR')) install.packages('genderBR'); library('genderBR')        # 1.1.0
if (!require('stringr')) install.packages('stringr'); library('stringr')           # 1.4.0
if (!require('dplyr')) install.packages('dplyr'); library('dplyr')                 # 1.0.9
if (!require('lubridate')) install.packages('lubridate'); library('lubridate')     # 1.7.10
if (!require('rdrobust')) install.packages('rdrobust'); library('rdrobust')        # 0.99.8
if (!require('rdd')) install.packages('rdd'); library('rdd')                       # 0.57
if (!require('plotrix')) install.packages('plotrix'); library('plotrix')           # 3.7-8
if (!require('lfe')) install.packages('lfe'); library('lfe')                       # 2.8-8
if (!require('stargazer')) install.packages('stargazer'); library('stargazer')     # 5.2.2
if (!require('logr')) install.packages('logr'); library('logr')                    # 1.3.4

# This one is really optional; you only need this if you are using RStudio and you don't want to set the working directory manually
if (!require('rstudioapi')) install.packages('rstudioapi'); library('rstudioapi')

# Removes all existing objects from memory
rm( list=ls() )

# If you are using RStudio, you can use the following line to set the working directory to folder where this script is saved.
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# Setting the working directory in this way will only work if you are using RStudio and this script is the "active document."
# If this does not work, you should set the working directory manually using the setwd() function. 
# The working directory should be the 2_scripts subfolder, where this script and all of the other scripts are saved


# Creates a new sub-folder for storing the log files
dir.create("../4_log", showWarnings = FALSE)



### Data Wrangling ###

# Warning: This code may take an hour or more to run

log_open("../4_log/log_wrangling.log")

# Builds the party membership datasets and codes the gender of party members
source("code_wrangling_MemberGender.R")

# Cleans the candidates dataset and matches the party initials in the candidates dataset to the initials in the party membership dataset
source("code_wrangling_candidates.R")

# Builds the RDD analysis datasets
source("code_wrangling_RDD.R")

# Builds additional analysis datasets
source("code_wrangling_Supplementary.R")

log_close()

### Analysis ###

# Creates new sub-folders for saving the results
dir.create("../3_output", showWarnings = FALSE)
dir.create("../3_output/figures", showWarnings = FALSE)
dir.create("../3_output/tables", showWarnings = FALSE)
dir.create("../3_output/appendix", showWarnings = FALSE)

log_open("../4_log/log_analysis.log")

# Performs the analyses in the main text of the article
source("code_analysis.R")

# Performs the analyses in the appendix / supplementary materials
source("code_appendix.R")

log_close()


